//
//  RCSearchAllVC.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-7-13.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCSearchAllVC.h"
#import "IDataModel.h"
#import "RCMerchantsListDataModel.h"
#import "IManager+ModuleSearchAll.h"
#import "RCMerchantListCell.h"
#import "RCMerchantDetailVC.h"

@interface RCSearchAllVC ()<IManagerDelegate>

@property(nonatomic,retain) UITableView *baseVCTableView;
@property (nonatomic, copy) NSString *searchTitle;
@property (nonatomic, retain) RCRecommendRestaurantListDataModel *dataModel;
@property (nonatomic,assign) int64_t requestID;
@property (nonatomic,retain) MBProgressHUDEx *progressHub;

@end

@implementation RCSearchAllVC

@synthesize dataModel = _dataModel;
@synthesize searchTitle = _searchTitle;
@synthesize baseVCTableView = _baseVCTableView;
@synthesize requestID = _requestID;
@synthesize progressHub = _progressHub;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id) initWithTitle:(NSString *)searchTitle
{
    self = [super init];
    if (self) {
        self.searchTitle = searchTitle;
    }
    return self;
}

- (void) dealloc
{
    self.dataModel = nil;
    self.searchTitle = nil;
    self.baseVCTableView = nil;
    self.progressHub = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.titleLabelText = @"搜索结果";
    self.navigationItem.titleView = [self createTitleView];
    if (!_baseVCTableView) {
        _baseVCTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, UIScreenHeight-64) style:UITableViewStylePlain];
        _baseVCTableView.backgroundColor = [UIColor clearColor];
        _baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _baseVCTableView.delegate = self;
        _baseVCTableView.dataSource = self;
    }
    [self.viewContainer addSubview:_baseVCTableView];
    [self refreshData];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark tableview
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataModel.listItems count];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = @"RCMerchantListCell";
    RCMerchantListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[RCMerchantListCell alloc] init] autorelease];
    }
    [cell bindData:[self.dataModel.listItems objectAtIndex:[indexPath row]]];
    return cell;
}

- (void)  tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    RCMerchant *merchant = [self.dataModel.listItems objectAtIndex:[indexPath row]];
    RCMerchantDetailVC *detail = [[RCMerchantDetailVC alloc] initWithMerchantID:merchant.merchantID];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}

#pragma
#pragma mark ServerData ----下拉刷新数据

-(void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _requestID = [[IManager sharedInstance] requestSearchAllCityWithName:self.searchTitle withDelegate:self];
}



#pragma
#pragma mark IManage Delegate
-(void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        self.dataModel = (RCRecommendRestaurantListDataModel *)dataModel;
        [_baseVCTableView reloadData];
    }
    
    
}

-(void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }
}

@end
